<?php

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

$sapitype = php_sapi_name(); 

ob_start(); 

  if($sapitype != "cli") { 
    require_once 'config.inc.php'; 
  } else {
    require_once BINDER_ROOT . '/config.inc.php';
    if(!file_exists(INCLUDE_PATH.'/DB.php')) {
      echo "Could Not Find DB.PHP (". INCLUDE_PATH ."/DB.php) , exiting \n";
      exit();   
    }
  }



if(!defined("INCLUDE_PATH")) {
	header("Location: setup.php"); 
}


// If setup.php exists, die;

if(file_exists('./setup.php')) { 
	echo "Please Remove setup.php to continue <br> "; 
	die;
}

if(!require_once(INCLUDE_PATH.'/DB.php')) {
	header("Location: setup.php"); 
} 

$db= new DB() ; 

$db->mSetdbHost(MYHOST);
$db->mSetdbName(MYDB);
$db->mSetdbPass(MYPASS);
$db->mSetdbUser(MYUSER);

$db->mConnect(); 

// Load the Globals from the Database. 


$sql = "SELECT * FROM binder_config"; 

$rs = $db->mRun($sql); 

while($row = mysql_fetch_assoc($rs)) {
  define("BASE_URL","http://".$row['web_hostname'] . $row['web_relpath']); #Base URL/Site Binder Resides on 
  define("BASEDIR",$row['web_servpath']);  #Path via the FS Binder sits on
  define("WEB_BASE",BASE_URL."/"); #Base URL from the Browser 
  define("TEMPLATE_PATH",BASEDIR."/templates/") ; 
  define("CTEMPLATE_PATH",BASEDIR."/templates_c/"); 
  define("VERSION","2.0alpha1") ; 
  define("COMPANY_NAME",$row['company_name']); 
	define("SUPPORT_EMAIL",$row['support_email']);
  define("DEFAULT_IP",trim($row['default_ip']));  
  define("DEFAULT_BOILER",BASEDIR."/boilerplates/defaultzone.tpl") ;
  define("DEFAULT_MX",trim($row['default_mx'])) ; 
  define("DEFAULT_MNAME",trim($row['soa_mname'])); 
  define("DEFAULT_RNAME",trim($row['soa_rname'])); 
}


/***********************************************************************************
  DO NOT EDIT BELOW THIS LINE. UNLESS YOU THINK YOU KNOW WHAT YOU'RE DOING
***********************************************************************************/

session_start();


require_once(INCLUDE_PATH."/smarty/Smarty.class.php") ; 
require_once(INCLUDE_PATH."/Auth.php") ; 
require_once(INCLUDE_PATH."/Errors.class.php"); 
require_once(INCLUDE_PATH."/FQDN.class.php"); 

$dom =& new FQDN() ; 

$tpl =& new Smarty() ; 
$tpl->assign("VERSION",VERSION); 
$tpl->assign("COMPANY_NAME",COMPANY_NAME);
$tpl->assign("SUPPORTEMAIL",SUPPORT_EMAIL); 
$tpl->assign("web_base",BASE_URL); 
$tpl->assign("title",$title); 



if(DEFAULT_IP == "") {
  $userlevel = $_SESSION['userlevel']; 
  if($userlevel == '0') { 
    $admin_error_msg = "<b> No Default IP Set You need to set this before you continue </b><br> ";
    $admin_error_msg .= "Please Go to : <a href='" . BASE_URL . "/admin/settings.php'> /admin/settings.php </a> and complete the configuration "; 
  } else {
    $admin_error_msg = "No Default IP Set, please contact: <a href='mailto:". SUPPORT_EMAIL ."'>"  . SUPPORT_EMAIL . " </a> and have them fix this problem<br>"; 
  }

  $tpl->assign("admin_error_message",$admin_error_msg); 
} 


/* 
   THESE ARE NECESSARY TO CONFIGURE SMARTY . IF NOT CONFIGURED PROPERLY
   SMARTY WILL NOT WORK.

*/

if("ADMIN" == $section) {
	$userlevel = $_SESSION['userlevel']; 
  $tpl->template_dir = TEMPLATE_PATH . "admin/";
  $tpl->compile_dir = CTEMPLATE_PATH;  
  $tpl->assign("userlevel",$userlevel); 
  $displaytpl = $tpl->template_dir . "/header.html"; 
} else {
	$userlevel = $_SESSION['userlevel']; 
	$tpl->assign("userlevel",$userlevel); 
  $tpl->template_dir = TEMPLATE_PATH ; 
  $tpl->compile_dir = CTEMPLATE_PATH ; 
  $displaytpl = "header.html"; 
}


$tpl->compile_dir = CTEMPLATE_PATH ; 
$userid = (empty($_SESSION['USERID'])) ? null : $_SESSION['USERID'];

/* Check to see if the user is logged in. If not, Make him */

if($sapitype != "cli") {
$a = new Auth(); 

/* If the Session Variable called Authorized, is not set, then show the login page */

$authorized = (empty($_SESSION['Authorized'])) ? null : $_SESSION['Authorized'];
if(($_SERVER['PHP_SELF'] != "logout.php") || ($_SERVER['PHP_SELF'] != "/remindme.php")) { 
  if(!$authorized) {
    /* Check to see if we've been posted a username or password */
    
    $username = (empty($_POST['username'])) ? null : $_POST['username'];
    $password = (empty($_POST['password'])) ? null : $_POST['password'];
    
    $a->mSetUsername($username); 
    $a->mSetPassword($password) ; 
    $a->misAuthorized(); 
    $a->mDisplayLoginForm(); 
    die; 
  } else {
    /* if he is authorized, get the firstname/lastname */
    $a->mSetUserId($userid); 
    $a->mLoadId(); 	
    
    $firstname = $a->_strFirstname; 
    $lastname  = $a->_strLastname;
    $email = $a->_strLastname; 
    
    $tpl->assign("firstname",$firstname); 
    $tpl->assign("lastname",$lastname); 
    $tpl->assign("email",$email) ; 
    $tpl->assign("userlevel",$userlevel); 
    
    
    $userowneddomains = $dom->loadDomainsFor($userid); 
    $tpl->assign("myfqdns",$userowneddomains) ; 


    
    /* Load the domains owned by this user */
    // This should not be done in the global.inc Moving to manage.php 
/* 
    $sql = "SELECT fqdnid,fqdn from fqdn where user='$userid' ORDER BY fqdn";

    $rs = $db->mRun($sql) ; 
    $fqdns = array(); 
    while($row=mysql_fetch_array($rs)) {
        $myfqdns[$row[0]] = $row[1] ; 
    }

*/
    /* If The posted mydomains is different than the current working domain.
    Change to posted Working domain */

    if(!isset($_SESSION['workingdomain'])) {
      $mydomain = $_POST['mydomains'];  
    } else {
      $mydomain = $_SESSION['workingdomain'];
    }


    if(empty($_POST['mydomains'])) {
      $pdom = null ; 
    } else {
      $pdom = $_POST['mydomains']; 
    } 


    if(($mydomain != $pdom) && (!is_null($pdom))){
      $mydomain = $_POST['mydomains']; 
    } else {
      $mydomain = $mydomain ; 
    } 

    $_SESSION['workingdomain'] = $mydomain ; 
    #$tpl->assign("myfqdns",$myfqdns) ; 
    $tpl->assign("mydomain",$mydomain) ; 
    
  }

} else {
    echo "Logging out "; 
} 


// What the hell is this and why is it here ?  - vl 3/25/05	


$dom->mSetId($mydomain) ; 


$dom->mLoad(); 

$fqdn = $dom->_strFqdn ; 

$tpl->assign("managingdomain",$fqdn); 




  $tpl->display($displaytpl) ; 
} 
/**
 * print_array : Wrapper for Var dump or print_r (does the <pre> </pre> stuff and determines wether to do var_dump or print_r
 * @param $dump: 
 * @param $$type="print_r" : 
 * 
 * @return 
 * @access 
 **/
function print_array($dump,$type="print_r") 
{
  echo "<pre class='ontop'>"; 
  if($type=="vd") {
    var_dump($dump);
  } else {
    print_r($dump);
  }

  echo "</pre>"; 


}

?>
